<?php
namespace App\Services\Fund\IntegralDistribute;
use App\Services\Fund\ValidateIntegralDistribute;
use App\Models\Fund\IntegralDistribute;
use App\Models\Certificate\Certificate;
use App\Services\Manage\Api;
use Illuminate\Support\Facades\DB;
class IntegralDistributeLogic
{
   /*添加资金派发积分的记录*/
    public function add($data): array 
    {
         $manageAppId = Api::getAppId();  //总后台app_id
        if($manageAppId != $data['app_id'] && $data['is_public'] == 1){ return verify_err(null, '只有亚数后台才可以添加公共读取的记录'); }
        unset($data['token']);
        $data['create_time'] = NOW;
        $data['update_time'] = NOW;
        $res = IntegralDistribute::insert($data);
        return auto_return($res, '成功添加派发积分记录');
    }

    /*获取某应用下的最新一条积分派发记录*/
    public function getOneNew($data): array 
    {
        $manageAppId = \app\manage\service\ApiBase::getAppId();
        $_whereOr = $manageAppId == $data['app_id'] ? true : '`app_id`=' . $data['app_id'] . ' OR FIND_IN_SET(' . $data['app_id'] . ',`other_app_read`) OR is_public = 1';
        $_where['status'] = 1;
        $oneNew = $this->model($this->integralDistributeModel)->where($_where)->where($_whereOr)->order('id DESC')->find();
        return auto_return($oneNew, '成功获取最新一条记录');
    }

    /*后台获取积分派发记录列表*/
    public function list($data): array 
    {
         $manageAppId = Api::getAppId();
         $app_id  = $data['app_id'];
        if($manageAppId == $data['app_id']){
              $list = IntegralDistribute::orderBy('id', 'desc')
                     ->paginate($data['listRows'], null, null, $data['page'])
                     ->toArray();
        }else{
            $list = IntegralDistribute::where(function ($query) use ($app_id ) {
                $query->orWhere('app_id', '=', $app_id )
                      ->orWhere('is_public', '=', 1)
                      ->orWhereRaw ("FIND_IN_SET($app_id ,other_app_read)");
                      })
                      ->orderBy('id', 'desc')
                      ->paginate($data['listRows'], null, null, $data['page'])
                      ->toArray();
        }
        return auto_return_page($list);
    }
     /*获取积分派发记录*/
   /* public function getDistributePageList($data): array 
    {
        if($data['certificate_id'] == '' && $data['status'] == '' && $data['distribute_date'] == '' && $data['keywords'] == ''){
            $list = Certificate::orderBy('id', 'desc')
                      ->paginate($data['listRows'], null, null, $data['page'])
                      ->toArray();
        }else{
        $keywords = $data['keywords'];
        $_where = [];
        $data['certificate_id'] !== '' && $_where[] = ['certificate_id', '=', $data['certificate_id']];
        $data['status'] !== '' && $_where[] = ['status', '=', $data['status']];
        $data['distribute_date'] !== '' && $_where[] = ['distribute_date', '=', $data['distribute_date']];

        if(!empty($_where) && $data['keywords'] !== ''){
           $list= Certificate::where($_where)->where(function($query) use ($keywords){
           $query->where('certificate_name','like',"%{$keywords}%")
                 ->orWhere('certificate_id', 'like',"%{$keywords}%")
                 ->orWhere('certificate_product_code', 'like',"%{$keywords}%");
                 }) 
                 ->orderBy('id', 'desc')
                 ->paginate($data['listRows'], null, null, $data['page'])
                 ->toArray();
        }else if(!empty($_where) && $data['keywords'] == ''){
             $list= Certificate::where($_where)
                 ->orderBy('id', 'desc')
                 ->paginate($data['listRows'], null, null, $data['page'])
                 ->toArray();
        }else if(empty($_where) && $data['keywords'] !== ''){
             $list= Certificate::where(function($query) use ($keywords){
             $query->where('certificate_name','like',"%{$keywords}%")
                 ->orWhere('certificate_id', 'like',"%{$keywords}%")
                 ->orWhere('certificate_product_code', 'like',"%{$keywords}%");
                 }) 
                 ->orderBy('id', 'desc')
                 ->paginate($data['listRows'], null, null, $data['page'])
                 ->toArray();
        }
        }          
        return auto_return_page($list);
    }*/
}